﻿using System;

namespace YXYLCommon
{
    public class StringDeal
    {

        public static string SubString(string source, int length)
        {
            return source.Length <= length ? source : source.Substring(0, length);
        }
        public static string GetZeroToEmpty(object o)
        {
            return Convert.ToInt32(o) == 0 ? "" : Convert.ToString(o);
        }

        /// <summary>
        /// 将以逗号拼接的字符串格式化为左右两侧均有逗号的,如"1,2,3"变为:",1,2,3,"
        /// 在数据库中将可用于charindex(','+id+',',",1,2,3,13,") 效果等同于 id in (1,2,3,13) 
        /// </summary>
        /// <returns></returns>
        public static string GetJoinIdsForDBIndexOf(string str)
        {
            if (str == "" || str == null)
            {
                return str;
            }
            if(str.Substring(0,1)!=",")
            {
                str = "," + str;
            }
            if(str.Substring(str.Length-1,1)!=",")
            {
                str = str + ",";
            }
            return str;
        }
        /// <summary>
        /// 将以逗号拼接的字符串格式化为右侧有逗号的,如"1,2,3"变为:"1,2,3,"
        /// 或数据库中可用于while循环分割操作
        /// </summary>
        /// <returns></returns>
        public static string GetJoinIdsForDBWhile(string str)
        {
            if (str == "" || str == null)
            {
                return str;
            }
            if (str.Substring(str.Length - 1, 1) != ",")
            {
                str = str + ",";
            }
            return str;
        }

        
    }
}
